/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.controllers.admin.wx;

import app.models.wx.Account;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.jfinal.plugin.ehcache.CacheKit;

import java.util.List;

/**
 * <p>
 * 微信帐号设置
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-02-12 13:41
 * @since JDK 1.6
 */
public class AccountController extends BasicController {

    public void index() {
        Account account;
        List<Account> accounts = Account.dao.list();
        if (accounts != null && !accounts.isEmpty()) {
            account = accounts.get(0);
        } else {
            account = new Account();
        }
        setAttr("account", account);
    }

    public void save() {
        Account account = getModel(Account.class);
        //保存/修改的时候放入缓存中
        CacheKit.put(Account.CACHE_NAME, Account.KEY, account);

        if (account.get("id") != null) {
            account.update();
        } else {
            account.save();
        }
        renderJson(AjaxMessage.ok("微信帐号设置成功"));
    }

}